﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BarcodePrint
{
    #region 定义设备类型枚举
    public enum DeviceType
    {
        COM = 0,
        LPT = 1,
        DRV = 2,
        TCP = 3
    }
    #endregion

    #region 定义打印机指令类型枚举
    public enum PrinterProgramLanguage
    {
        ZPL = 0,
        EPL = 1,
        CPCL = 2,
        None = 3
    }
    #endregion

    public static class EnumUtil
    {
        /// <summary>
        /// 获取指定枚举,
        /// 与枚举的索引组成匿名对象加入到 List 中返回，new { Text = text, Value = value }
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <returns>List</returns>
        public static List<object> GetEnumToList(Type enumType)
        {
            if (enumType.IsEnum)
            {
                int value;
                string text;
                List<object> resultList = new List<object>();

                foreach (FieldInfo fi in enumType.GetFields())
                {
                    if (fi.FieldType.IsEnum)
                    {
                        text = fi.Name;
                        value = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                        resultList.Add(new { Text = text, Value = value });
                    }
                }
                return resultList;
            }
            else
            {
                throw new ArgumentException("必须是 enum 类型");
            }
        }
    }
}
